package com.demo.easyExcel.fillColor.version3_3_2;

//import com.alibaba.excel.metadata.CellData;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.List;

/**
 * https://blog.csdn.net/scholartang/article/details/108024953
 *
 * @date 01/22/2021 15:46
 */
public class CellColorCellWriteHandler implements CellWriteHandler {

    /**
     * map
     * key：第i行
     * value：第i行中单元格索引集合
     */
    private HashMap<Integer, List<Integer>> map;

    /**
     * 颜色
     */
    private Short colorIndex;

    /**
     * 无参构造
     */
    public CellColorCellWriteHandler() {

    }

    /**
     * 有参构造
     */
    public CellColorCellWriteHandler(HashMap<Integer, List<Integer>> map, Short colorIndex) {
        this.map = map;
        this.colorIndex = colorIndex;
    }


    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
        System.out.println(4);
        int i = cell.getColumnIndex();
        //不处理第一行
        List<Integer> integers = map.get(cell.getRowIndex());
        if (integers != null && integers.size() > 0) {
            if (integers.contains(i)) {
                Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                CellStyle cellStyle = workbook.createCellStyle();

                cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                cell.setCellStyle(cellStyle);
                context.getFirstCellData().setWriteCellStyle(null);
            }
        }
    }
}

